package com.cskaoyan.service.back.ad;

import com.cskaoyan.bean.bo.back.ad.AdBo;
import com.cskaoyan.bean.po.BaseParam;
import com.cskaoyan.bean.po.MarketAd;
import com.cskaoyan.bean.po.MarketAdExample;
import com.cskaoyan.bean.vo.back.ad.AdListVo;
import com.cskaoyan.mapper.MarketAdMapper;
import com.cskaoyan.util.ParamUtils;
import com.cskaoyan.util.StringToLikeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * 广告服务impl
 *
 * @author 李然
 * @date 2022/10/12
 */
@Service
public class AdvertisingServiceImpl implements AdvertisingService {

    @Autowired
    MarketAdMapper marketAdMapper;

    /**
     * 获得广告列表
     *
     * @param parameter 参数
     * @param name      名字
     * @param content   内容
     * @return {@link AdListVo}
     */
    @Override
    public AdListVo<MarketAd> getAdList(BaseParam parameter, String name, String content) {
        PageHelper.startPage(parameter.getPage(), parameter.getLimit());
        MarketAdExample example = new MarketAdExample();
        MarketAdExample.Criteria criteria = example.createCriteria();
        if (ParamUtils.isValid(name)) {
//            criteria.andNameLike("%" + name + "%");
            criteria.andNameLike(StringToLikeUtil.addPercentSign(name));
        }
        if (ParamUtils.isValid(content)) {
            criteria.andContentLike(StringToLikeUtil.addPercentSign(content));
        }
        example.setOrderByClause(parameter.getSort() + " " + parameter.getOrder());
        criteria.andDeletedEqualTo(false);
        List<MarketAd> ads = marketAdMapper.selectByExample(example);
        PageInfo<MarketAd> adsPageInfo = new PageInfo<>(ads);
//        return new AdListVo(((int) pageInfo.getTotal()), pageInfo.getPages(), pageInfo.getPageSize(), pageInfo.getPageNum(), ads);
        return new AdListVo<>(adsPageInfo,ads);
    }

    /**
     * 删除
     *
     * @param adBo 广告博
     * @return int
     */
    @Override
    public int delete(AdBo adBo) {

        MarketAd record = new MarketAd();
        record.setId(adBo.getId());
        record.setDeleted(true);
        return marketAdMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 更新
     *
     * @param ad 广告
     * @return {@link AdBo}
     */
    @Override
    public AdBo update(AdBo ad) {
        MarketAd marketAd = new MarketAd();

        marketAd.setUpdateTime((new Date()));

//        此处为springframework中的beanUtils
//        ad --> marketAd
        BeanUtils.copyProperties(ad, marketAd);

        marketAdMapper.updateByPrimaryKeySelective(marketAd);
        ad.setUpdateTime(new Date());
        return ad;

    }

    /**
     * 创建
     *
     * @param ad 广告
     * @return {@link MarketAd}
     */
    @Override
    public MarketAd create(AdBo ad) {
        MarketAd marketAd = new MarketAd();

//        此处为springframework中的beanUtils
//        ad --> marketAd
        BeanUtils.copyProperties(ad, marketAd);
        marketAd.setAddTime(new Date());
        marketAd.setUpdateTime(new Date());
        marketAd.setDeleted(false);
        int i = marketAdMapper.insert(marketAd);
        return marketAd;
    }

}
